package com.yxh.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yxh.base.PageEntity;
import com.yxh.dao.ChangeMapper;
import com.yxh.dao.UserMapper;
import com.yxh.dao.ViciousMapper;
import com.yxh.pojo.Change;
import com.yxh.pojo.User;
import com.yxh.service.ChangeService;
import com.yxh.service.FlowManageService;
import com.yxh.util.SnowflakeIdWorker;
import com.yxh.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created by asus on 2018/6/26.
 */

@Service
@Transactional
public class ChangeServiceImpl  implements ChangeService {

    @Autowired
    private ChangeMapper changeMapper;

    @Autowired
    private ViciousMapper viciousMapper;

    @Autowired
    private FlowManageService flowManageService;


    @Autowired
    private UserMapper userMapper;

    @Override
    public int save(Change record) {

        try {
            String imgs=record.getImgId();
            if(StringUtil.isNotEmpty(imgs)){
                String id= SnowflakeIdWorker.creatId();
                flowManageService.insertImgs(imgs,id);
                record.setImgId(id);
            }
            return changeMapper.insertSelective(record);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int update(Change record) {

        try {

            return changeMapper.updateByPrimaryKeySelective(record);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int delete(String id) {
        return 0;
    }

    @Override
    public void batchDelete(List<Change> records) {

    }

    @Override
    public Change get(String id) {
        return null;
    }

    @Override
    public Change get(Change record) {
        return null;
    }

    @Override
    public List<Change> getList(Change record) {
        return null;
    }

    @Override
    public List<Change> getAllList(Change record) {
        return null;
    }

    @Override
    public List<Change> getListPage(PageEntity<Change> record) {
        return null;
    }

    @Override
    public PageInfo getInformation(Change record, Integer page, Integer pageSize) {

        PageHelper.startPage(page, pageSize);
        List<Map<String,Object>> menuList =changeMapper.selectByVicious(record);
        PageInfo pageInfo = new PageInfo(menuList);
        pageInfo.setList(menuList);
        return pageInfo;
    }

    @Override
    public Map<String, Object> getById(String viciousId) {
        Map<String, Object> m=changeMapper.selectByPrimaryKey(viciousId);

        if(m!=null&&m.get("auditingId")!=null)

        m.put("auditingName", flowManageService.nameBack(m.get("auditingId").toString()));

        return m;
    }

    @Override
    public Map<String, Object> getNewById(String viciousId) {
        Map<String, Object> m=changeMapper.selectByPrimaryKey(viciousId);
        if(m.get("imgId")!=null){
            m.put("imgs",viciousMapper.getListImgs((String) m.get("imgId")));
        }
        if(m!=null&&m.get("auditingId")!=null)
            m.put("auditingName", flowManageService.nameBack(m.get("auditingId").toString()));
        return m;
    }


}
